<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
    defined ('IN_AGORA') or die;

    class TaskController extends AgoraProfileController {
		function _default() {
			//Agora::prepareUser($this->user, $this->agora_config['o_ranks']);
			$user_helper = & $this->helper('user');
			$this->user = $user_helper->prepareUserAvatar($this->user);
			$this->user = $user_helper->prepareUserTitle($this->user);
			$this->user = $user_helper->prepareUserUrl($this->user);

			$parser = & $this->helper('parser');
			$this->user['signature'] = $parser->parseSignature($this->user['signature']);
			$this->user['aboutme'] = $parser->parseSignature($this->user['aboutme']);

			$this->user['email'] = JHTML::_('email.cloak', $this->user['email']);

			if (!empty($this->user['birthday'])) {
				$date_format = $this->agora_config['o_date_format'];
				// this option must hide the entire birth date no only the year!
				//if (!empty($this->user['hide_age'])) $date_format = trim(preg_replace('[ \./-]?y+[ \./-]?', '', $date_format));
				$this->user['birthday'] = Model::formatDate(date($date_format, $this->user['birthday']));
			}
			
			$warning_model = &Model::getInstance('WarningModel');
			$warnings = $warning_model->loadByUser($this->user['id']);
			$ban_model = &Model::getInstance('BanModel');
			$this->user['banned'] = $ban_model->isBanned($this->user['username']);
			if ($this->user['banned']) {
			    $ban = $ban_model->load($this->user['username'],'username');
			    $this->view->assign('ban',$ban);
			}
			
			$same_user = (int)$this->agora_user['id'] == (int)$this->user['id'] ? true : false;			
			$this->view->assign('same_user',$same_user);
			
			$this->view->assign('warnings',$warnings);
			
			$user = &Model::getInstance('UserModel');
			$has_aup = $user->hasAUP();
			$this->view->assign('has_aup',$has_aup);
			if($has_aup) {
				$josid = (int)$this->user['jos_id'];
				$result = $user->getAUPoints(array($josid));
				$points = (is_array($result) && count($result) > 0 && array_key_exists($josid, $result)) ? $result[$josid]['points'] : 0;
				$this->view->assign('points',$points);
			}
		}
		
		function add_warning() {
			$warning_model = &Model::getInstance('WarningModel');
			$warnings = $warning_model->loadAll();
			$this->view->assign('warnings',$warnings);

			$this->view->template = 'add_warning';
		}

		function save_warning() {
			$form = Agora::getPostVar('form');

			$warnings_model = &Model::getInstance('WarningModel');
			$warnings_model->addToUser($this->user['id'], $form['warning_id'],$form['message']);
			$this->redirect();
		}
		
		function set_ban() {
			$this->view->template = 'set_ban';
		}

		function ban() {
			$ban_model = &Model::getInstance('BanModel');
			$form = Agora::getPostVar('form');

			$e = & $form['expire'];
			$expire = intval($e['days'])*3600 * 24 + intval($e['hours']) * 3600 + intval($e['minutes']) * 60;
			$expire = AGORA_TIME + $expire;

			$ban_model->banUser($this->user['username'],$form['message'],$expire);

			$message = $form['message'] ? $form['message'] : JText::_('BAN_EMAIL_NOREASON');
			$mail = JFactory::getMailer();
			$o_email_sender = trim($this->agora_config['o_email_sender']);
			if ($o_email_sender) {
				$sender_arr = explode(',', $o_email_sender);
				if (count($sender_arr) == 2) {
					$mail->setSender(array($sender_arr[0], $sender_arr[1]));
				} else {
					$mail->setSender($o_email_sender);
				}
			}
			$mail->addRecipient($this->user['email']);
			$mail->setSubject(JText::_('BAN_EMAIL_SUBJECT'));
			$mail->setBody(JText::sprintf('BAN_EMAIL_BODY', $message));
			$mail->IsHTML(true);
			$mail->Send();

			$this->redirect();
		}

		function unban() {
			$ban_model = &Model::getInstance('BanModel');
			$ban_model->unBanUser($this->user['username']);

			$mail = JFactory::getMailer();
			$o_email_sender = trim($this->agora_config['o_email_sender']);
			if ($o_email_sender) {
				$sender_arr = explode(',', $o_email_sender);
				if (count($sender_arr) == 2) {
					$mail->setSender(array($sender_arr[0], $sender_arr[1]));
				} else {
					$mail->setSender($o_email_sender);
				}
			}
			$mail->addRecipient($this->user['email']);
			$mail->setSubject(JText::_('UNBAN_EMAIL_SUBJECT'));
			$mail->setBody(JText::_('UNBAN_EMAIL_BODY'));
			$mail->IsHTML(true);
			$mail->Send();

			$this->redirect();
		}
	}
?>
